﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace UIPrototyper
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        bool operate = false;
        Point startPosition;
        UserControl selectedUC = null;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.Source.GetType().BaseType == typeof(UserControl))
            {
                this.operate = true;
                startPosition = e.GetPosition(this);
                selectedUC = (UserControl)e.Source;
            }
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (operate && selectedUC != null)
            {
                Point mousePosition = e.GetPosition(this);

                switch (((SelectionUC)selectedUC).Operation)
                {
                    case Operations.Move:
                        selectedUC.Margin = new Thickness(selectedUC.Margin.Left + (mousePosition.X - startPosition.X),
                            selectedUC.Margin.Top + (mousePosition.Y - startPosition.Y), 0, 0);
                        break;

                    case Operations.ResizeDTopLeft:
                        selectedUC.Margin = new Thickness(selectedUC.Margin.Left + (mousePosition.X - startPosition.X),
                            selectedUC.Margin.Top + (mousePosition.Y - startPosition.Y), 0, 0);
                        selectedUC.Width = (selectedUC.Width - (mousePosition.X - startPosition.X)) < 0 ? 0 : selectedUC.Width - (mousePosition.X - startPosition.X);
                        selectedUC.Height = (selectedUC.Height - (mousePosition.Y - startPosition.Y)) < 0 ? 0 :selectedUC.Height - (mousePosition.Y - startPosition.Y);                        

                            break;

                    case Operations.ResizeDTopRight:
                        selectedUC.Margin = new Thickness(selectedUC.Margin.Left,
                            selectedUC.Margin.Top + (mousePosition.Y - startPosition.Y), 0, 0);
                        selectedUC.Width = (selectedUC.Width + (mousePosition.X - startPosition.X)) < 0 ? 0 :selectedUC.Width + (mousePosition.X - startPosition.X);
                        selectedUC.Height = (selectedUC.Height - (mousePosition.Y - startPosition.Y)) < 0 ? 0 :selectedUC.Height - (mousePosition.Y - startPosition.Y);
                        break;

                    case Operations.ResizeDBottomLeft:
                        selectedUC.Margin = new Thickness(selectedUC.Margin.Left + (mousePosition.X - startPosition.X),
                            selectedUC.Margin.Top, 0, 0);
                        selectedUC.Width = (selectedUC.Width - (mousePosition.X - startPosition.X)) < 0 ? 0 :selectedUC.Width - (mousePosition.X - startPosition.X);
                        selectedUC.Height = (selectedUC.Height + (mousePosition.Y - startPosition.Y)) < 0 ? 0 :selectedUC.Height + (mousePosition.Y - startPosition.Y);
                        break;

                    case Operations.ResizeDBottomRight:
                        selectedUC.Width = (selectedUC.Width + (mousePosition.X - startPosition.X)) < 0 ? 0 :selectedUC.Width + (mousePosition.X - startPosition.X);
                        selectedUC.Height = (selectedUC.Height + (mousePosition.Y - startPosition.Y)) < 0 ? 0 :selectedUC.Height + (mousePosition.Y - startPosition.Y);
                        break;

                    case Operations.ResizeVTop:
                        selectedUC.Margin = new Thickness(selectedUC.Margin.Left, 
                            selectedUC.Margin.Top + (mousePosition.Y - startPosition.Y), 0, 0);
                        selectedUC.Height = (selectedUC.Height - (mousePosition.Y - startPosition.Y)) < 0 ? 0 :selectedUC.Height - (mousePosition.Y - startPosition.Y);
                        break;

                    case Operations.ResizeHLeft:
                        selectedUC.Margin = new Thickness(selectedUC.Margin.Left + (mousePosition.X - startPosition.X),
                            selectedUC.Margin.Top, 0, 0);
                        selectedUC.Width = (selectedUC.Width - (mousePosition.X - startPosition.X)) < 0 ? 0 :selectedUC.Width - (mousePosition.X - startPosition.X);
                        break;

                    case Operations.ResizeHRight:
                        selectedUC.Width = (selectedUC.Width + (mousePosition.X - startPosition.X)) < 0 ? 0 :selectedUC.Width + (mousePosition.X - startPosition.X);
                        break;

                    case Operations.ResizeVBottom:
                        selectedUC.Height = (selectedUC.Height + (mousePosition.Y - startPosition.Y)) < 0 ? 0 :selectedUC.Height + (mousePosition.Y - startPosition.Y);
                        break;
                }

                startPosition = mousePosition;
            }
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.operate = false;
            selectedUC = null;
        }
    }
}
